import { createTextNode } from "./react";

export class Iterator {
  constructor(root) {
    const node = this.#createNode(root);
    this.nextNode = node;
    if (this.#hashChildren(node)) {
      const children = this.#getChildren(node);
      this.#setChildrenRelation(children);
      if (node.alternate) {
        const alternateChildren = node.alternate.props.children;
        children.forEach((child) => {
          console.log("🚀 ~ Iterator ~ constructor ~ child:", child);
        });
        // console.log(
        //   "🚀 ~ Iterator ~ constructor ~ alternateChildren:",
        //   alternateChildren
        // );
      }
    }
  }

  next() {
    const node = this.nextNode;
    // 有孩子，给孩子设置parent和sibling
    if (this.#hashChildren(node)) {
      const children = this.#getChildren(node);
      this.#setChildrenRelation(children);

      // if (node.alternate) {
      //   children.forEach((child, i) => {
      //     child.alternate = node.alternate.props.children[i];
      //     console.log("🚀 ~ Iterator ~ children.forEach ~ child:", child);
      //   });
      // }

      // 设置下一个节点
      this.nextNode = children[0];
    } else {
      // 设置下一个节点，兄弟节点或其他树枝上的兄弟节点
      this.nextNode = this.#getSibling(node);
    }

    return node;
  }

  hasNext() {
    return this.nextNode !== null;
  }

  #createNode(leaf) {
    const isTextNode = typeof leaf === "string" || typeof leaf === "number";
    if (isTextNode) {
      //TODO 这里应该重构，Iterator应该接受一个函数用来处理节点的形状
      leaf = createTextNode(leaf);
    } else if (typeof leaf.type === "function") {
      // debugger;
      const { children, ...args } = leaf.props;
      const n = leaf.type(args);
      leaf.props.children = [leaf.type(args)];

      // debugger;
    }
    // console.log("🚀 ~ Iterator ~ #createNode ~ leaf:", leaf);

    return Object.assign({}, leaf, { parent: null, sibling: null });
  }

  #setChildrenRelation(children) {
    // 虽然执行了3次循环，但是能将设置节点，设置parent，设置sibling解耦
    children.forEach((child, idx) => {
      // debugger;
      children[idx] = this.#createNode(child);

      // const parentAlternate = child.parent?.alternate;
      // if (parentAlternate) {
      //   children[idx].alternate = parentAlternate.props.children[idx];
      //   console.log("🚀 ~ Iterator ~ children.forEach ~ child:", children[idx]);
      // }
      // children[idx].alternate = child.parent?.alternate?.props?.children[idx];
      // if (child.parent?.alternate) {
      //   const parentAlternate = child.parent?.alternate;
      //   children[idx].alternate = parentAlternate.props.children[idx];
      //   console.log(
      //     "🚀 ~ Iterator ~ children.forEach ~ children[idx]:",
      //     children[idx]
      //   );

      //   children[idx];
      //   debugger;
      // }
    });
    this.#setChildrenParent(children);
    this.#setChildrenSibling(children);
  }

  #setChildrenParent(children) {
    children.forEach((child, i) => {
      children[i].parent = this.nextNode;
    });
  }

  #setChildrenSibling(children) {
    children.reduce((pre, cur, idx) => {
      if (idx > 0) {
        pre.sibling = cur;
      }
      return cur;
    }, null);
  }

  #hashChildren(node) {
    return node.props.children?.length > 0;
  }
  #getChildren(node) {
    return node.props.children;
  }

  #getSibling(node) {
    if (node.sibling) {
      return node.sibling;
    }
    // 没有parent，最后的节点，
    if (!node.parent) return null;
    // 有parent，查找parent的sibling
    return this.#getSibling(node.parent);
  }
}
